package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import com.sanqing.model.BookInfo;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class BookAddIFrame {
    public bookIFrame addBookIFrame = new bookIFrame();

    /*BookInfo bookInfo;
    JTextField ISBN = new JTextField(13);
    JComboBox bookTypeComBox;
    JTextField bookName = new JTextField(20);
    JTextField writeText = new JTextField(20);
    JTextField translatorText = new JTextField(20);
    JTextField publisherText = new JTextField(20);
    JTextField dateText = new JTextField(20);
    JTextField priceText = new JTextField(20);
    JTextField all_countText = new JTextField(20);


    public BookAddIFrame() {
        super();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        ISBN.setFont(new Font("宋体", Font.PLAIN, 25));
        bookName.setFont(new Font("宋体", Font.PLAIN, 25));
        writeText.setFont(new Font("宋体", Font.PLAIN, 25));
        translatorText.setFont(new Font("宋体", Font.PLAIN, 25));
        publisherText.setFont(new Font("宋体", Font.PLAIN, 25));
        dateText.setFont(new Font("宋体", Font.PLAIN, 25));
        priceText.setFont(new Font("宋体", Font.PLAIN, 25));
        all_countText.setFont(new Font("宋体", Font.PLAIN, 25));

        final BorderLayout borderLayout = new BorderLayout();           //边框布局管理器
        getContentPane().setLayout(borderLayout);           //设置布局
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭
        setTitle("添加图书");                 //设置窗体标题
        setBounds(100, 100, 700, 350); //设置窗体位置和大小
        final JPanel mainPanel = new JPanel();          //创建中心面板
        mainPanel.setBorder(new EmptyBorder(5, 0, 170, 0));    //设置边框
        final GridLayout gridLayout = new GridLayout(3, 6);  //创建表格布局管理器
        gridLayout.setVgap(2);          //设置组件之间的垂直距离
        gridLayout.setHgap(2);          //设置组件之间的平行距离
        mainPanel.setLayout(gridLayout);
        getContentPane().add(mainPanel);

        bookInfo = new BookInfo();
        final JLabel ISBNLabel = new JLabel("ISBN:");
        ISBNLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        ISBNLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(ISBNLabel);
        //创建廿号文本框 "
        ISBN.setDocument(new MyDocument(13));                //书号文本框最大输入值为 13 ISBN.setColumns(13);//设蜜文本框长度
        //ISBN.addKeyListener(new ISBNkeyListener());                       //注册监听器 ISBN.addFocusListener(new ISBNFocusListener。);//注册监听器
        mainPanel.add(ISBN);
        final JLabel bookTypeLabel = new JLabel("类别:");    //创建书籍类别标签
        bookTypeLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        bookTypeLabel.setHorizontalAlignment(SwingConstants.CENTER);  //对齐方式 bookTypeLabeLsetText ("类别：")；	//设置标签文本
        mainPanel.add(bookTypeLabel);    //添加到中心面板

        ArrayList<BookType> list = Dao.selectBookCategory();    //从数据库中取出图书类别
        ComBoxMyModel comBoxMyModel = new ComBoxMyModel();
        comBoxMyModel.setLst(list);
        bookTypeComBox = new JComboBox(comBoxMyModel);
        bookTypeComBox.addItemListener(new booktypeComBoxListener());
        bookTypeComBox.setFont(new Font("宋体", Font.PLAIN, 25));
        mainPanel.add(bookTypeComBox);

        JLabel bookNameLabel = new JLabel("书名:");
        bookNameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        bookNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //bookName.addActionListener();
        mainPanel.add(bookNameLabel);
        mainPanel.add(bookName);

        JLabel writeLabel = new JLabel("作者:");
        writeLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        writeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //writeText.addActionListener()
        mainPanel.add(writeLabel);
        mainPanel.add(writeText);

        JLabel translatorLabel = new JLabel("译者:");
        translatorLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        translatorLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(translatorLabel);
        mainPanel.add(translatorText);

        JLabel publisherLabel = new JLabel("出版社:");
        publisherLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        publisherLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(publisherLabel);
        mainPanel.add(publisherText);

        JLabel dateLabel = new JLabel("出版日期:");
        dateLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        dateLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(dateLabel);
        mainPanel.add(dateText);

        JLabel priceLabel = new JLabel("价格:");
        priceLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        priceLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(priceLabel);
        mainPanel.add(priceText);

        JLabel all_countLabel = new JLabel("总数:");
        all_countLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        all_countLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(all_countLabel);
        mainPanel.add(all_countText);

        JPanel sumPanel = new JPanel();
        sumPanel.setLayout(new FlowLayout());
        JButton submit = new JButton("添加");
        JButton reset = new JButton("重置");
        submit.setFont(new Font("宋体", Font.PLAIN, 25));
        reset.setFont(new Font("宋体", Font.PLAIN, 25));
        submit.addActionListener(new submitAction());
        reset.addActionListener(new resetBut());
        sumPanel.add(submit);
        sumPanel.add(reset);
        getContentPane().add(sumPanel, BorderLayout.SOUTH);
    }*/
    public BookAddIFrame() {
        addBookIFrame.submit.setText("添加");
        addBookIFrame.reset.setText("重置");
        addBookIFrame.setTitle("添加图书");

        //  图书添加按钮事件过程  不允许为空值
        addBookIFrame.submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!addBookIFrame.ISBN.getText().equals("")) {
                    addBookIFrame.bookInfo.setISBN(addBookIFrame.ISBN.getText());
                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "请输入ISBN码");
                    return;
                }
                if (addBookIFrame.bookInfo.getTypeid() == 0) {
                    JOptionPane.showMessageDialog(addBookIFrame, "请选择图书类别");
                    return;
                }
                if (!addBookIFrame.bookName.getText().equals("")) {
                    addBookIFrame.bookInfo.setBookname(addBookIFrame.bookName.getText());
                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "请输入书名");
                    return;
                }
                if (!addBookIFrame.writeText.getText().equals("")) {
                    addBookIFrame.bookInfo.setWriter(addBookIFrame.writeText.getText());
                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "请输作者");
                    return;
                }
                /*if (!addBookIFrame.translatorText.getText().equals("")) {
                    addBookIFrame.bookInfo.setTranslator(addBookIFrame.translatorText.getText());
                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "请输入译者");
                    return;
                }*/
                if (!addBookIFrame.publisherText.getText().equals("")) {
                    addBookIFrame.bookInfo.setPublisher(addBookIFrame.publisherText.getText());
                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "请输入译者");
                    return;
                }
                if (!addBookIFrame.dateText.getText().equals("")) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                    try {
                        Date date = sdf.parse(addBookIFrame.dateText.getText());
                        addBookIFrame.bookInfo.setDate(date);
                    } catch (ParseException parseException) {
                        parseException.printStackTrace();
                    }

                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "请输入出版日期");
                    return;
                }
                if (!addBookIFrame.priceText.getText().equals("")) {
                    addBookIFrame.bookInfo.setPrice(Double.parseDouble(addBookIFrame.priceText.getText()));
                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "请输入价格");
                    return;
                }
                if (!addBookIFrame.all_countText.getText().equals("")) {
                    addBookIFrame.bookInfo.setAll_count(Integer.parseInt(addBookIFrame.all_countText.getText()));
                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "请输入总数");
                    return;
                }
                System.out.println(addBookIFrame.bookInfo.toString());


                if (Dao.addNewBook(addBookIFrame.bookInfo)) {
                    JOptionPane.showMessageDialog(addBookIFrame, "添加成功！");
                } else {
                    JOptionPane.showMessageDialog(addBookIFrame, "添加失败");
                }
            }
        });


        // 图书添加重置按钮事件过程
        addBookIFrame.reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addBookIFrame.bookInfo = new BookInfo();
                addBookIFrame.ISBN.setText("");
                addBookIFrame.bookTypeComBox.setSelectedIndex(-1);
                addBookIFrame.bookName.setText("");
                addBookIFrame.writeText.setText("");
                addBookIFrame.translatorText.setText("");
                addBookIFrame.publisherText.setText("");
                addBookIFrame.priceText.setText("");
                addBookIFrame.dateText.setText("");
                addBookIFrame.all_countText.setText("");
            }
        });

    }


    /*private class submitAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!addBookIFrame.ISBN.getText().equals("")) {
                addBookIFrame.bookInfo.setISBN(addBookIFrame.ISBN.getText());
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请输入ISBN码");
                return;
            }
            if (addBookIFrame.bookInfo.getTypeid() == 0) {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请选择图书类别");
                return;
            }
            if (!addBookIFrame.bookName.getText().equals("")) {
                addBookIFrame.bookInfo.setBookname(addBookIFrame.bookName.getText());
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请输入书名");
                return;
            }
            if (!addBookIFrame.writeText.getText().equals("")) {
                addBookIFrame.bookInfo.setWriter(addBookIFrame.writeText.getText());
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请输作者");
                return;
            }
            if (!addBookIFrame.translatorText.getText().equals("")) {
                addBookIFrame.bookInfo.setTranslator(addBookIFrame.translatorText.getText());
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请输入译者");
                return;
            }
            if (!addBookIFrame.publisherText.getText().equals("")) {
                addBookIFrame.bookInfo.setPublisher(addBookIFrame.publisherText.getText());
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请输入译者");
                return;
            }
            if (!addBookIFrame.dateText.getText().equals("")) {
                addBookIFrame.bookInfo.setDate(addBookIFrame.dateText.getText());
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请输入出版日期");
                return;
            }
            if (!addBookIFrame.priceText.getText().equals("")) {
                addBookIFrame.bookInfo.setPrice(Double.parseDouble(addBookIFrame.priceText.getText()));
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请输入价格");
                return;
            }
            if (!addBookIFrame.all_countText.getText().equals("")) {
                addBookIFrame.bookInfo.setAll_count(Integer.parseInt(addBookIFrame.all_countText.getText()));
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "请输入总数");
                return;
            }
            System.out.println(addBookIFrame.bookInfo.toString());


            if (Dao.addNewBook(addBookIFrame.bookInfo)) {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "添加成功！");
            } else {
                JOptionPane.showMessageDialog(BookAddIFrame.this, "添加失败");
            }

        }
    }

    private class resetBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            addBookIFrame.bookInfo = new BookInfo();
            addBookIFrame.ISBN.setText("");
            addBookIFrame.bookTypeComBox.setSelectedIndex(-1);
            addBookIFrame.bookName.setText("");
            addBookIFrame.writeText.setText("");
            addBookIFrame.translatorText.setText("");
            addBookIFrame.publisherText.setText("");
            addBookIFrame.priceText.setText("");
            addBookIFrame.dateText.setText("");
            addBookIFrame.all_countText.setText("");
        }
    }*/
}

